package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class SupplierServiceImp implements SupplierService {
    @Autowired
    private SupplierDao supplierDao;
    @Override
    // 获取到 所有的 供应商 的 信息
    public Map<String, Object> supplierList(Integer page, Integer rows, String supplierName) {
        HashMap<String, Object> map = new HashMap<>();
         Long total = supplierDao.total(page,rows,supplierName);
        List<Supplier> list = supplierDao.supplierList((page-1)*rows,rows,supplierName);
        map.put("rows",list);
        map.put("total",total);
        return map;
    }
   // 实现 供应商 的 更新和 删除
    @Override
    public void saveOrUpdate(Long supplierId, Supplier supplier) {
        // 实现保存
         if(ObjectUtils.isEmpty(supplierId)){
            supplierDao.save(supplier);
         }else {
             supplierDao.update(supplier);
         }
    }
  // 实现批量删除供货商
    @Override
    public void delete(String ids) {
       if(!StringUtils.isEmpty(ids)){
           String[] split = ids.split(",");
           if(split!=null){
               supplierDao.delete(Arrays.asList(split));
           }

       }
    }
}
